﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Minecraft.BLL.mysql;
using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Model;
using Minecraft.Model.Backpack;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;

namespace Minecraft.ServerHall.Cmd
{
	public class BackpackGoodsInsert : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Backpack_BackpackGoodsInsert;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<BackpackGoodsInsertReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：参数错误）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：未登录）" });
				return;
			}
			session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：未开发）" });
			return;

			////物品来源判断
			//if (!MinecraftConfig.enumGoodsSourceBelongsToTypeList.Any(m => m == req.enumGoodsBelongsToType))
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：物品来源不正确，前端传入的物品来源为【{req.enumGoodsBelongsToType.ToString()}】）" });
			//	return;
			//}
			//if (req.DropId <= 0)
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "参数 DropId <= 0" });
			//	return;
			//}
			//if (req.GoodsItemId.IsNullOrWhiteSpace())
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：GoodsItemId 参数为空）" });
			//	return;
			//}

			//if (req.Num <= 0)
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "参数 Num <= 0" });
			//	return;
			//}
			//if (req.WastageValue <= 0)
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "参数 WastageValue <= 0" });
			//	return;
			//}
			//if (req.guid.IsNullOrWhiteSpace())
			//{
			//	req.guid = "";
			//}

			//var dropinfo = session.minecraftSessionInfo.dropinfoList.FirstOrDefault(m => m.DropId == req.DropId);
			//if (dropinfo == null)
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：参数 DropId值为【{req.DropId}】的掉落数据不存在数据库中）" });
			//	return;
			//}
			//var dropinfoGoodsInfoList = dropinfo.GetDropinfoGoodsInfoList_Convert();
			//var dropinfoGoodsInfo = dropinfoGoodsInfoList.FirstOrDefault(m => m.GoodsItemId == req.GoodsItemId);
			//if (dropinfoGoodsInfo == null)
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：数据库中DropId值为【{req.DropId}】的掉落数据中不存在itemId为【{req.GoodsItemId}】的掉落物品）" });
			//	return;
			//}
			//if (req.Num > dropinfoGoodsInfo.Num)
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：数据库中DropId值为【{req.DropId}】的掉落数据中itemId为【{req.GoodsItemId}】的掉落物品的个数不够，数据库中剩余个数为【{dropinfoGoodsInfo.Num}】，客户端传递的个数为（req.Num）【{req.Num}】）" });
			//	return;
			//}

			//var timeNow = DateTimeOffset.Now;

			//var seconds = Convert.ToInt32((timeNow - dropinfo.DropTime).TotalSeconds);
			//int s = MinecraftConfig.DropGoodsTimeKeepSeconds;
			//if (seconds > s)
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：掉落时间超时，显示时间为【{s} s】，但是时间已经过了【{seconds} s】）" });
			//	return;
			//}

			//dropinfoGoodsInfo.Num -= req.Num;
			//dropinfo.TransformToDropItemIdAndNum_Convert(dropinfoGoodsInfo);
			//if (!dropinfo.GetDropinfoGoodsInfoList_Convert().Any())
			//{
			//	//如果掉落数据中没有物品了，则移除该掉落信息
			//	session.minecraftSessionInfo.dropinfoList.RemoveAll(m => m.DropId == dropinfo.DropId);
			//}
			////if (!CsvConfig.itemsList.Any(m => m.Id == req.GoodsItemId))
			//Mod_Items mod_Items = CsvConfig.GetMod_ItemsInfoById(req.GoodsItemId);
			//if (mod_Items == null)
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"【{req.GoodsItemId}】物品itemid不存在于服务器配置表中" });
			//	return;
			//}
			////从配置中读取指定物品存放数量上限
			//var goodsItemMaxNum = session.GetMaxCellNumConvert(req.GoodsItemId);
			////加载背包物品goodsItem信息
			//List<BackpackGoodsItemInfo> goodsItemInfoList = session.GetBackpackGoodsItemInfoList(req.enumGoodsBelongsToType, req.guid, out BaseResp baseResp);
			//if (baseResp.RespLevel != RespLevelEnum.Success)
			//{
			//	session.Send(defCommand, req, baseResp);
			//	return;
			//}

			////添加操作影响的positionOptList
			//List<BackpackGoodsPositionOpt> positionOptList = new List<BackpackGoodsPositionOpt>();

			////找到能够满足存放物品条件的位置信息
			//Func<BackpackGoodsItemInfo, bool> predicate = m =>
			//	(m.GoodsNum <= 0 || m.GoodsItemId == req.GoodsItemId && m.GoodsNum < goodsItemMaxNum) && m.guid == req.guid;

			//var goodsItemInfo2 = goodsItemInfoList.FirstOrDefault(predicate);
			//if (goodsItemInfo2 == null)
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "格子已经满了，或者guid对应的信息不存在" });
			//	return;
			//}
			//goodsItemInfo2.GoodsItemId = req.GoodsItemId;

			////获取可以插入的num数
			//var canInsertNum = Math.Min(req.Num, session.GetMaxCellNumConvert(goodsItemInfo2.GoodsItemId) - goodsItemInfo2.GoodsNum);
			//goodsItemInfo2.GoodsNum += canInsertNum;

			//var goodsInfo = session.minecraftSessionInfo.goodsList.FirstOrDefault(m =>
			//m.enumGoodsBelongsToType_Convert == goodsItemInfo2.enumGoodsBelongsToType
			//&& m.GoodsPosition == goodsItemInfo2.GoodsPosition
			//&& m.guid == goodsItemInfo2.guid
			//);
			////更新内存
			//if (goodsInfo == null)
			//{
			//	goodsInfo = new GoodsModel
			//	{
			//		BelongsTo = (int)goodsItemInfo2.enumGoodsBelongsToType,
			//		GoodsId = StringHelper.GetGuidStr(),
			//		GoodsItemId = goodsItemInfo2.GoodsItemId,
			//		GoodsPosition = goodsItemInfo2.GoodsPosition,
			//		guid = goodsItemInfo2.guid,
			//		Num = goodsItemInfo2.GoodsNum,
			//		PlayerId = session.minecraftSessionInfo.player.PlayerId,
			//		WastageValue = 100,
			//	};
			//	session.minecraftSessionInfo.goodsList.Add(goodsInfo);
			//}
			//else
			//{
			//	goodsInfo.Num = goodsItemInfo2.GoodsNum;
			//}

			////找到受影响的物品位置信息
			//Func<GoodsModel, bool> predicateBeAffected = m =>
			//	(positionOptList.Any(n => n.enumGoodsBelongsToType == m.enumGoodsBelongsToType_Convert
			//	&& n.GoodsPosition == m.GoodsPosition && n.guid == m.guid));
			//var posGoodsinfoList = session.minecraftSessionInfo.goodsList.Where(predicateBeAffected).GetBackpackGoodsCellInfo();

			//var resp = new BackpackGoodsInsertResp
			//{
			//	posGoodsinfoList = posGoodsinfoList,
			//	enumGoodsBelongsToType = req.enumGoodsBelongsToType,
			//	guid = req.guid,
			//};
			//session.Send(defCommand, req, resp);
		}
	}
}
